package com.ebt.m.customer.util;

import com.ebt.m.customer.db.CustomerInteraction;
import com.ebt.m.customer.entity.CustomerSimple;
import com.ebt.m.customer.item.Contact;
import com.ebt.m.customer.listener.IContactComparable;
import com.ebt.m.customer.model.CustomerFromContact;
import com.ebt.m.customer.model.CustomerGet;
import com.ebt.m.customer.model.CustomerModel;
import com.ebt.m.customer.model.CustomerModelDetail;
import com.ebt.m.customer.model.CustomerModelNew;
import com.ebt.m.customer.net.json.CustomerDynamicJson;
import com.ebt.m.customer.net.json.CustomerNoteJson;
import com.ebt.m.data.entity.EntityCustomerNote;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class CompareHelper {

    public static final Collator COLLATOR = Collator.getInstance();
    public static final Comparator<CustomerModel> COMPARATOR_CUSTOMER_MODEL;
    public static final Comparator<CustomerModelNew> COMPARATOR_CUSTOMER_MODEL_NEW;
    public static final Comparator<CustomerSimple> COMPARATOR_CUSTOMER_SIMPLE;
    public static final Comparator<CustomerModelDetail> COMPARATOR_CUSTOMER_MODEL_DETAIL_NAME;
    public static final Comparator<CustomerModelDetail> COMPARATOR_CUSTOMER_MODEL_DETAIL_TIME;
    public static final Comparator<CustomerFromContact> COMPARATOR_CUSTOMER_FROM_CONTACT;
    public static final Comparator<Contact> COMPARATOR_CONTACT;
    public static final Comparator<Contact> COMPARATOR_CONTACT_RAW_ID;
    public static final Comparator<CustomerGet> COMPARATOR_CUSTOMER_GET_CONTACT_RAW_ID;
    public static final Comparator<EntityCustomerNote> COMPARATOR_CUSTOMER_NOTE;
    public static final Comparator<CustomerNoteJson> COMPARATOR_CUSTOMER_NOTE_JSON;
    public static final Comparator<CustomerDynamicJson> COMPARATOR_CUSTOMER_DYNAMIC_JSON;
    public static final Comparator<String> COMPARATOR_STRING_ASC;
    public static final Comparator<String> COMPARATOR_STRING_DESC;

    ///////////////////////////////////////////////

    public static final Comparator<CustomerModelDetail> COMPARATOR_CUSTOMER_MODEL_DETAIL_UUID;
    public static final Comparator<CustomerModelDetail> COMPARATOR_CUSTOMER_MODEL_DETAIL_DATE;

    static {
        COMPARATOR_CUSTOMER_MODEL = new Comparator<CustomerModel>() {
            @Override
            public final int compare(CustomerModel a, CustomerModel b) {
                return COLLATOR.compare(a.sortKeyString, b.sortKeyString);
            }
        };

        COMPARATOR_CUSTOMER_MODEL_NEW = new Comparator<CustomerModelNew>() {
            @Override
            public final int compare(CustomerModelNew a, CustomerModelNew b) {
                return COLLATOR.compare(a.sortKeyString, b.sortKeyString);
            }
        };
        COMPARATOR_CUSTOMER_SIMPLE = new Comparator<CustomerSimple>() {
            @Override
            public final int compare(CustomerSimple a, CustomerSimple b) {
                return COLLATOR.compare(a.sortKeyString, b.sortKeyString);
            }
        };

        COMPARATOR_CUSTOMER_MODEL_DETAIL_NAME = new Comparator<CustomerModelDetail>() {
            @Override
            public final int compare(CustomerModelDetail a, CustomerModelDetail b) {
                return COLLATOR.compare(a.name, b.name);
            }
        };

        COMPARATOR_CUSTOMER_MODEL_DETAIL_TIME = new Comparator<CustomerModelDetail>() {
            @Override
            public final int compare(CustomerModelDetail a, CustomerModelDetail b) {
                if (a.lastActiveTime == null) {
                    a.lastActiveTime = 0L;
                }
                if (b.lastActiveTime == null) {
                    b.lastActiveTime = 0L;
                }
                int compareLastActiveTime = COLLATOR.compare(
                        String.valueOf(b.lastActiveTime), String.valueOf(a.lastActiveTime));
                if (compareLastActiveTime != 0) {
                    return compareLastActiveTime;
                }
                int compareName = COLLATOR.compare(a.name, b.name);
                if (compareName != 0) {
                    return compareName;
                }
                int compareUuid = COLLATOR.compare(a.customerUuid, b.customerUuid);
                if (compareUuid != 0) {
                    return compareUuid;
                }
                //如果都相等，就按照type排序，按照type从大到小排列
                return COLLATOR.compare(String.valueOf(b.type), String.valueOf(a.type));
            }
        };

        //////////////////////////////////////////////

        COMPARATOR_CUSTOMER_MODEL_DETAIL_UUID = new Comparator<CustomerModelDetail>() {
            @Override
            public final int compare(CustomerModelDetail a, CustomerModelDetail b) {
                // 先按照UUID排序
                int compareUuid = COLLATOR.compare(a.customerUuid, b.customerUuid);
                if (compareUuid != 0) {
                    return compareUuid;
                }
                //如果都相等，就按照TIME排序
                return COLLATOR.compare(String.valueOf(b.lastActiveTime), String.valueOf(a.lastActiveTime));
            }
        };

        COMPARATOR_CUSTOMER_MODEL_DETAIL_DATE = new Comparator<CustomerModelDetail>() {
            @Override
            public final int compare(CustomerModelDetail a, CustomerModelDetail b) {
                return COLLATOR.compare(String.valueOf(b.lastActiveTime), String.valueOf(a.lastActiveTime));
            }
        };

        /////////////////////////////////////////////

        COMPARATOR_CUSTOMER_FROM_CONTACT = new Comparator<CustomerFromContact>() {
            @Override
            public final int compare(CustomerFromContact a, CustomerFromContact b) {
                int nameAndId = COLLATOR.compare(a.contactSortKey + a.contactRawId, b.contactSortKey + b.contactRawId);
                if (nameAndId != 0) {
                    return nameAndId;
                } else {
                    return COLLATOR.compare(a.contactSortKey + a.contactRawId + (a.contactIgnore == null ? 0 : a.contactIgnore),
                            b.contactSortKey + b.contactRawId + (b.contactIgnore == null ? 0 : b.contactIgnore));
                }
            }
        };

        COMPARATOR_CONTACT = new Comparator<Contact>() {
            @Override
            public final int compare(Contact a, Contact b) {
                return COLLATOR.compare(a.sortKeyString + a.rawContactId, b.sortKeyString + b.rawContactId);
            }
        };

        COMPARATOR_CONTACT_RAW_ID = new Comparator<Contact>() {
            @Override
            public final int compare(Contact a, Contact b) {
                return COLLATOR.compare(a.rawContactId, b.rawContactId);
            }
        };

        COMPARATOR_CUSTOMER_GET_CONTACT_RAW_ID = new Comparator<CustomerGet>() {
            @Override
            public final int compare(CustomerGet a, CustomerGet b) {
                return COLLATOR.compare(a.rawContactId, b.rawContactId);
            }
        };

        COMPARATOR_CUSTOMER_NOTE = new Comparator<EntityCustomerNote>() {
            @Override
            public final int compare(EntityCustomerNote a, EntityCustomerNote b) {
                //大的在前
                return COLLATOR.compare(String.valueOf(b.getLastUpdateTime().getTime()), String.valueOf(a.getLastUpdateTime().getTime()));
            }
        };

        COMPARATOR_CUSTOMER_NOTE_JSON = new Comparator<CustomerNoteJson>() {
            @Override
            public final int compare(CustomerNoteJson a, CustomerNoteJson b) {
                return COLLATOR.compare(String.valueOf(b.createTime), String.valueOf(a.createTime));
            }
        };

        COMPARATOR_CUSTOMER_DYNAMIC_JSON = new Comparator<CustomerDynamicJson>() {
            @Override
            public final int compare(CustomerDynamicJson a, CustomerDynamicJson b) {
                return COLLATOR.compare(String.valueOf(b.activeTime), String.valueOf(a.activeTime));
            }
        };

        COMPARATOR_STRING_ASC = new Comparator<String>() {
            @Override
            public final int compare(String a, String b) {
                return COLLATOR.compare(a, b);
            }
        };

        COMPARATOR_STRING_DESC = new Comparator<String>() {
            @Override
            public final int compare(String a, String b) {
                return COLLATOR.compare(b, a);
            }
        };
    }

    private CompareHelper() {
    }

    /**
     * 获取从all队列剔除ignores后的队列
     *
     * @param listAlls
     * @param listIgnores
     * @return
     */
    public static <E extends Comparable> ArrayList<E> getSurvives(ArrayList<E> listAlls, ArrayList<E> listIgnores) {

        ArrayList<E> listAllSurvives = new ArrayList();

        int sizeAll = listAlls.size();
        int sizeIgnore = listIgnores.size();
        int indexAll = 0;
        int indexIgnore = 0;

        for (; indexIgnore < sizeIgnore; ) {
            for (; indexAll < sizeAll; ) {

                int compareRet = listIgnores.get(indexIgnore).compareTo(listAlls.get(indexAll));

                if (compareRet < 0) {
                    indexIgnore++;
                } else if (compareRet == 0) {
//					indexIgnore ++;
                    indexAll++;
                } else if (compareRet > 0) {
                    listAllSurvives.add(listAlls.get(indexAll));
                    indexAll++;
                }

                if (indexIgnore >= sizeIgnore) {
                    for (int i = indexAll; i < sizeAll; i++) {
                        listAllSurvives.add(listAlls.get(i));
                    }
                    break;
                }
            }
            if (indexAll >= sizeAll) {
                break;
            }
        }

        return listAllSurvives;
    }

    /**
     * 获取从all队列剔除ignores后的队列
     *
     * @param listAlls
     * @param listIgnores
     * @return
     */
    public static ArrayList<IContactComparable> getSurvivesOfContact(ArrayList<IContactComparable> listAlls, ArrayList<IContactComparable> listIgnores) {

        ArrayList<IContactComparable> listAllSurvives = new ArrayList();

        int sizeAll = listAlls.size();
        int sizeIgnore = listIgnores.size();
        int indexAll = 0;
        int indexIgnore = 0;

        for (; indexIgnore < sizeIgnore; ) {
            for (; indexAll < sizeAll; ) {

                int compareRet = listIgnores.get(indexIgnore).getSortKey().compareTo(listAlls.get(indexAll).getSortKey());

                if (compareRet < 0) {
                    indexIgnore++;
                } else if (compareRet == 0) {
                    //如果姓名相同，但是号码不同，则同样需要加入到返回的集合中去
                    if (!checkStringEqual(listIgnores.get(indexIgnore).getRawContactId(), listAlls.get(indexAll).getRawContactId())) {
                        listAllSurvives.add(listAlls.get(indexAll));
                    }
//					indexIgnore ++;
                    indexAll++;
                } else if (compareRet > 0) {
                    listAllSurvives.add(listAlls.get(indexAll));
                    indexAll++;
                }

                if (indexIgnore >= sizeIgnore) {
                    for (int i = indexAll; i < sizeAll; i++) {
                        listAllSurvives.add(listAlls.get(i));
                    }
                    break;
                }
            }
            if (indexAll >= sizeAll) {
                break;
            }
        }

        return listAllSurvives;
    }

    /**
     * 获取从all队列剔除ignores后的队列
     *
     * @param listAlls
     * @param listIgnores
     * @return
     */
    public static List<Contact> getSurvivesOfContactFromCustomerContact(List<Contact> listAlls, List<CustomerFromContact> listIgnores) {

        List<Contact> listAllSurvives = new ArrayList();

        int sizeAll = listAlls.size();
        int sizeIgnore = listIgnores.size();
        int indexAll = 0;
        int indexIgnore = 0;

        for (; indexIgnore < sizeIgnore; ) {
            for (; indexAll < sizeAll; ) {
                int compareRet = CompareHelper.COLLATOR.compare(listIgnores.get(indexIgnore).getSortKey(), listAlls.get(indexAll).getSortKey());
//				int compareRet = listIgnores.get(indexIgnore).getSortKey().compareTo(listAlls.get(indexAll).getSortKey());
                if (compareRet < 0) {
                    indexIgnore++;
                } else if (compareRet == 0) {
                    //如果姓名相同，但是号码不同，则同样需要加入到返回的集合中去
                    if (!checkStringEqual(listIgnores.get(indexIgnore).getRawContactId(), listAlls.get(indexAll).getRawContactId())) {
                        listAllSurvives.add(listAlls.get(indexAll));
                    }
//					indexIgnore ++;
                    indexAll++;
                } else if (compareRet > 0) {
                    listAllSurvives.add(listAlls.get(indexAll));
                    indexAll++;
                }

                if (indexIgnore >= sizeIgnore) {
                    for (int i = indexAll; i < sizeAll; i++) {
                        listAllSurvives.add(listAlls.get(i));
                    }
                    break;
                }
            }
            if (indexAll >= sizeAll) {
                break;
            }
        }

        return listAllSurvives;
    }

    /**
     * 获取从all队列剔除ignores后的队列
     *
     * @param listAlls
     * @param listIgnores
     * @return
     */
    /*public static List<Contact> setIgnoreMarksOfContactFromCustomerContact(List<Contact> listAlls, List<CustomerFromContact> listIgnores){

        for(Contact c : listAlls){
            Log.d("72999","contact name : " + c.getName() + " id : " + c.getRawContactId());
        }

        for(CustomerFromContact c : listIgnores){
            Log.d("72999","ignores name : " + c.contactName + " id : " + c.getRawContactId());
        }

        List<Contact> listAllSurvives = new ArrayList();

        int sizeAll = listAlls.size();
        int sizeIgnore = listIgnores.size();
        int indexAll = 0;
        int indexIgnore = 0;

        for(; indexIgnore < sizeIgnore; ){
            for(; indexAll < sizeAll;){

                int compareRet = CompareHelper.COLLATOR.compare(listIgnores.get(indexIgnore).getSortKey(), listAlls.get(indexAll).getSortKey());
//				int compareRet = listIgnores.get(indexIgnore).getSortKey().compareTo(listAlls.get(indexAll).getSortKey());
                if(compareRet < 0){
                    indexIgnore ++;
                }else if(compareRet == 0){
                    String sortKey = listIgnores.get(indexIgnore).getSortKey();
                    //如果姓名相同，但是rawContactId不同，则同样需要加入到返回的集合中去
                    int x;
                    for(x = indexAll + 1; x < sizeAll; x++){
                        if(checkStringEqual(listAlls.get(x).getSortKey(), sortKey)){
                            x++;
                        }else{
                            break;
                        }
                    }
                    //indexAll中，新的名字从x开始

                    int y;
                    for(y = indexIgnore + 1; y < sizeIgnore; y++){
                        if(checkStringEqual(listIgnores.get(y).getSortKey(), sortKey)){
                            y++;
                        }else{
                            break;
                        }
                    }
                    //indexIgnore中，新的名字从y开始
                    //----------------------------

                    for(; indexIgnore < y; ) {
                        for (; indexAll < x; ) {
                            int compareRawIdRet = CompareHelper.COLLATOR.compare(
                                    listIgnores.get(indexIgnore).contactRawId, listAlls.get(indexAll).rawContactId);
                            if (compareRawIdRet < 0) {
                                indexIgnore++;
                            } else if (compareRet == 0) {
                                Integer hasIgnored = listIgnores.get(indexIgnore).getContactIgnore();
                                if (hasIgnored != null && hasIgnored == 1) {
                                    Contact c = listAlls.get(indexAll);
                                    c.hasIgnored = true;
                                    listAllSurvives.add(c);
                                }
                                indexIgnore++;
                                indexAll++;
                            } else if (compareRet > 0) {
                                listAllSurvives.add(listAlls.get(indexAll));
                                indexAll++;
                            }

                            if (indexIgnore >= y) {
                                for (int i = indexAll; i < x; i++) {
                                    listAllSurvives.add(listAlls.get(i));
                                }
                                break;
                            }
                        }
                        if (indexAll >= x) {
                            break;
                        }
                    }
                    //----------------------------

                    *//*if(!checkStringEqual(listIgnores.get(indexIgnore).getRawContactId(), listAlls.get(indexAll).getRawContactId())){
                        //这里需要进行一次小的循环？
                        listAllSurvives.add(listAlls.get(indexAll));
                    }else{//姓名相同、号码相同，则需要将其置为ignored=true
                        Log.d("72999","setIgnoreMarksOfContactFromCustomerContact sortkey: " +  listIgnores.get(indexIgnore).getSortKey()
                                + " rawID" + listIgnores.get(indexIgnore).getRawContactId());
                        Integer hasIgnored = listIgnores.get(indexIgnore).getContactIgnore();
                        if(hasIgnored != null && hasIgnored == 1) {
                            Contact c = listAlls.get(indexAll);
                            c.hasIgnored = true;
                            listAllSurvives.add(c);
                        }
                        indexIgnore++;
                    }
//					indexIgnore ++;
                    indexAll++;*//*
                }else if(compareRet > 0){
                    listAllSurvives.add(listAlls.get(indexAll));
                    indexAll++;
                }

                if(indexIgnore >= sizeIgnore){
                    for(int i = indexAll; i < sizeAll; i++){
                        listAllSurvives.add(listAlls.get(i));
                    }
                    break;
                }
            }
            if(indexAll >= sizeAll){
                break;
            }
        }

        return listAllSurvives;
    }*/
    public static List<Contact> setIgnoreMarksOfContactFromCustomerContact(List<Contact> listAlls, List<CustomerFromContact> listIgnores, boolean showHasIgnores, boolean showHasImports) {

        List<Contact> listAllSurvives = new ArrayList();

        int sizeAll = listAlls.size();
        int sizeIgnore = listIgnores.size();
        int indexAll = 0;
        int indexIgnore = 0;

//        for(Contact item : listAlls){
//            Log.d("0804","before contact name : " + item.getName() + "  rawId : " + item.getRawContactId());
//        }

        for (; indexIgnore < sizeIgnore; ) {
            for (; indexAll < sizeAll; ) {
                int compareRet = CompareHelper.COLLATOR.compare(listIgnores.get(indexIgnore).getSortKey(), listAlls.get(indexAll).getSortKey());
                if (compareRet < 0) {
                    indexIgnore++;
                } else if (compareRet == 0) {
                    String sortKey = listIgnores.get(indexIgnore).getSortKey();
                    int x;
                    for (x = indexAll + 1; x < sizeAll; x++) {
                        if (checkStringEqual(listAlls.get(x).getSortKey(), sortKey)) {
//                            x++;
                        } else {
                            break;
                        }
                    }
                    int y;
                    for (y = indexIgnore + 1; y < sizeIgnore; y++) {
                        if (checkStringEqual(listIgnores.get(y).getSortKey(), sortKey)) {
//                            y++;
                        } else {
                            break;
                        }
                    }
                    for (; indexIgnore < y; ) {
                        for (; indexAll < x; ) {
                            int compareRawIdRet = CompareHelper.COLLATOR.compare(
                                    listIgnores.get(indexIgnore).contactRawId.trim(), listAlls.get(indexAll).rawContactId.trim());
                            if (compareRawIdRet < 0) {
                                indexIgnore++;
                            } else if (compareRawIdRet == 0) {
                                Integer hasIgnored = listIgnores.get(indexIgnore).getContactIgnore();
                                if (hasIgnored != null && hasIgnored == 1) {
                                    if (showHasIgnores) {
                                        Contact c = listAlls.get(indexAll);
                                        c.hasIgnored = true;
                                        listAllSurvives.add(c);
                                    }
                                } else {
                                    if (showHasImports) {
                                        Contact c = listAlls.get(indexAll);
                                        c.hasImported = true;
                                        listAllSurvives.add(c);
                                    }
                                }
                                indexIgnore++;
                                indexAll++;
                            } else if (compareRawIdRet > 0) {
                                listAllSurvives.add(listAlls.get(indexAll));
                                indexAll++;
                            }

                            if (indexIgnore >= y) {
                                for (int i = indexAll; i < x; i++) {
                                    listAllSurvives.add(listAlls.get(i));
                                }
                                indexAll = x;
                                break;
                            }
                        }
                        if (indexAll >= x) {
                            break;
                        }
                    }
                } else if (compareRet > 0) {
                    listAllSurvives.add(listAlls.get(indexAll));
                    indexAll++;
                }
                if (indexIgnore >= sizeIgnore) {
                    for (int i = indexAll; i < sizeAll; i++) {
                        listAllSurvives.add(listAlls.get(i));
                    }
                    break;
                }
            }
            if (indexAll >= sizeAll) {
                break;
            }
        }
        return listAllSurvives;
    }

    public static List<CustomerFromContact> removeSameCustomerFromContact(List<CustomerFromContact> inputList) {
        if (inputList == null) {
            return inputList;
        }
        List<CustomerFromContact> outputList = new ArrayList<>(inputList.size());

        CustomerFromContact prevItem = null;
        for (CustomerFromContact currItem : inputList) {
            if (currItem == null) {
                continue;
            }
            //去除ignore==0而customerUuid为空的情况
            if((currItem.contactIgnore == null || currItem.contactIgnore == 0)
                    && (currItem.customerUuid == null || currItem.customerUuid.trim().length() == 0 )){
                //导入了，但是uuid为空，这种情况出错了
                continue;
            }
            if (prevItem == null) {
                outputList.add(currItem);
            } else {
                if (checkStringEqual(currItem.contactName, prevItem.contactName)
                        && checkStringEqual(currItem.contactRawId, prevItem.contactRawId)
                        /*&& checkIntegerEqual(currItem.contactIgnore, prevItem.contactIgnore)*/) {
                    continue;
                } else {
                    outputList.add(currItem);
                }
            }
            prevItem = currItem;
        }
        return outputList;
    }

    public static boolean checkIntegerEqual(Integer input0, Integer input1) {
        if (input0 == null || input0 == 0) {
            if (input1 == null || input1 == 0) {
                return true;
            } else {
                return false;
            }
        } else {
            if (input1 == null) {
                return false;
            } else {
                return input0.equals(input1);
            }
        }
    }

    public static boolean checkStringEqual(String input0, String input1) {
        if (input0 == null) {
            if (input1 == null) {
                return true;
            } else {
                return false;
            }
        }
        return input0.equals(input1);
    }

}
